//
//  AppDelegate.swift
//  MyWeiBo
//
//  Created by 牛牛很牛 on 15/12/15.
//  Copyright © 2015年 曾勇召. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        //设置按钮外观
        self.setupAppearance()
        
        //加载账号
        //let userAccount = ZYZUserAcount.loadUserAccount()
        
        
        
        window = UIWindow(frame: UIScreen.mainScreen().bounds)
        window?.backgroundColor = UIColor.whiteColor()
        //let tabBarVC = ZYZTabBarController()
        window?.rootViewController = defaultViewController()
 //       window?.rootViewController = ZYZNewFeatureController()
        window?.makeKeyAndVisible()
        
        return true
    }
    
    
    private func defaultViewController() -> UIViewController{
        if !ZYZUserAcount.userLogin{
            //如果没登陆，进入MainViewController(Main)
            return ZYZTabBarController()
        }
        //如果登陆了，判断是否是有新版本
        return isNewVision() ? ZYZNewFeatureController() : ZYZWelcomeController()
    }
    
    
    //判断是否是新版本
    private func isNewVision() ->Bool{
        let currentVision = NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"] as! String
        
        //获取上一次保存的版本
        let sanBoxVisionKey = "sanBoxVision"
        let sanBoxVision = NSUserDefaults.standardUserDefaults().stringForKey(sanBoxVisionKey)
        
        //当有新版本时，保存并返回
        if currentVision != sanBoxVision{
            NSUserDefaults.standardUserDefaults().setObject(currentVision, forKey: sanBoxVisionKey)
            NSUserDefaults.standardUserDefaults().synchronize()
            return true
        }else{
            return false
        }
    
    }
    
    
    //切换控制器
    func switchRootViewController(isMain : Bool) {
        window?.rootViewController = isMain ? ZYZTabBarController() : ZYZWelcomeController()
    }
    
    
    
    //设置全局导航栏按钮
  private  func setupAppearance(){
        UINavigationBar.appearance().tintColor = UIColor.orangeColor()
    }




}

